package com.metrics.ctrls.imp;

import static com.web.utils.SingletonData.*;

import java.io.PrintWriter;
import java.util.*;

import javax.servlet.ServletOutputStream;

import com.database.objects.*;
import com.metrics.ctrls.IReportCtrl;
import com.metrics.extractors.*;
import com.metrics.objects.Metric;
import com.metrics.reports.ReportGenerator;

public class ReportCtrl implements IReportCtrl {

	private static final long serialVersionUID = 5945788518089983193L;

	private ReportGenerator reportGenerator;

	private MetricExtractorMap metricExtractorMap;

	@Override
	public void generateMetricsReport(ServletOutputStream out, Project project, Repository repository, Metric metric) {
		PrintWriter pw = new PrintWriter(out);
		MetricExtractor extractor = metricExtractorMap.get(metric);

		List<String> lines = reportGenerator.generate(project, repository, extractor);
		for (String line : lines) {
			pw.println(line);
		}
		pw.close();
	}

	@Override
	public void generateModelReport(ServletOutputStream out, String data) {
		PrintWriter pw = new PrintWriter(out);
		pw.println("DEVELOPER$CONTRIBUTION SIZE$CONTRIBUTION QUALITY");

		String[] array = data.split(SEPARATOR_1);
		for (int i = 0; i < array.length; i++) {
			String row = array[i];
			pw.println(row.replace(SEPARATOR_2, ReportGenerator.SEPARATOR));
		}

		pw.close();
	}

	public void setReportGenerator(ReportGenerator reportGenerator) {
		this.reportGenerator = reportGenerator;
	}

	public void setMetricExtractorMap(MetricExtractorMap metricExtractorMap) {
		this.metricExtractorMap = metricExtractorMap;
	}

}